package com.situ.mall.job;

import com.situ.mall.config.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.util.Set;

@Configuration
public class DeleteImageJob {
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 */1 * * * ?")
    public void deleteImageJob() {
        System.out.println("DeleteImageJob.deleteImageJob");
        //根据Redis保存的两个set集合做差值计算，获取所有的垃圾图片
        Set<String> set = redisTemplate.opsForSet().difference(RedisConstant.UPLOAD_IMAGE, RedisConstant.UPLOAD_IMAGE_TO_DB);
        if (set != null) {
            for (String imageName : set) {
                //删除硬盘上图片
                File file = new File("D:\\mypic\\" + imageName);
                file.delete();

                redisTemplate.opsForSet().remove(RedisConstant.UPLOAD_IMAGE, imageName);
                System.out.println("删除图片： " + imageName);
            }
        }

    }
}
